package com.cmccgz.udacs_landlord_webapp.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cmccgz.baselibrary.utils.StringUtils;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.bean.PushRequestBean;
import com.cmccgz.udacs_landlord_webapp.interfaces.OnRvItemClickListenter;

import java.util.List;

public class AccreditAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final LayoutInflater inflater;
    private Context mContext;
    private List<PushRequestBean>datas;
    private OnRvItemClickListenter<PushRequestBean>listenter;
    public AccreditAdapter(Context mContext, List<PushRequestBean> datas, OnRvItemClickListenter<PushRequestBean>listenter) {
        this.mContext = mContext;
        this.datas = datas;
        inflater = LayoutInflater.from(mContext);
        this.listenter=listenter;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case -1:
                return new EmptyViewHolder(inflater.inflate(R.layout.item_empty,parent,false));
            case 1:
                return new NormalViewHolder(inflater.inflate(R.layout.item_accredit,parent,false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case -1:
                ((EmptyViewHolder) holder).tv_empty.setText("暂无数据，下拉可刷新！");
                break;
            case 1:
                bindNormalHolder((NormalViewHolder) holder, position);
                break;
        }
    }

    private void bindNormalHolder(final NormalViewHolder holder, final int position) {
        final PushRequestBean pushRequestBean = datas.get(position);
        holder.tv_name.setText(StringUtils.isEmpty(pushRequestBean.v_name)?"未知":pushRequestBean.v_name);
        holder.tv_remark.setText(StringUtils.isEmpty(pushRequestBean.remark)?"":pushRequestBean.remark);
        holder.tv_time.setText(StringUtils.isEmpty(pushRequestBean.time)?"":pushRequestBean.time);
        holder.tv_has_used.setText(pushRequestBean.has_use?"已授权":"未授权");
        holder.tv_has_used.setTextColor(ContextCompat.getColor(mContext,pushRequestBean.has_use?R.color.blue_d:R.color.red));

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listenter!=null) {
                    listenter.onItemClick(pushRequestBean,position,holder.itemView);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas==null||datas.size()==0?1:datas.size();
    }

    @Override
    public int getItemViewType(int position) {
        return datas==null||datas.size()==0?-1:1;
    }

    private class NormalViewHolder extends RecyclerView.ViewHolder{

        private final TextView tv_time;
        private final TextView tv_name;
        private final TextView tv_remark;
        private final TextView tv_has_used;

        public NormalViewHolder(View itemView) {
            super(itemView);
            tv_time = ((TextView) itemView.findViewById(R.id.tv_time));
            tv_name = ((TextView) itemView.findViewById(R.id.tv_name));
            tv_remark = ((TextView) itemView.findViewById(R.id.tv_remark));
            tv_has_used = ((TextView) itemView.findViewById(R.id.tv_has_used));
        }
    }

    private class EmptyViewHolder extends RecyclerView.ViewHolder {
        private final TextView tv_empty;

        public EmptyViewHolder(View itemView) {
            super(itemView);
            tv_empty = ((TextView) itemView.findViewById(R.id.tv_empty));
        }

    }
}
